package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.entity.Knowledge;
import com.qst.crop.dao.KnowledgeDao;
import com.qst.crop.service.KnowledgeService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * (Knowledge)表服务实现类
 *
 * @author Wlj
 * @since 2025-09-02 15:27:55
 */
@Service("knowledgeService")
public class KnowledgeServiceImpl implements KnowledgeService {
    @Resource
    private KnowledgeDao knowledgeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param knowledgeId 主键
     * @return 实例对象
     */
    @Override
    public Knowledge queryById(Integer knowledgeId) {
        return this.knowledgeDao.queryById(knowledgeId);
    }

    /**
     * 分页查询
     *
     * @param knowledge 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Knowledge> queryByPage(Knowledge knowledge, PageRequest pageRequest) {
        long total = this.knowledgeDao.count(knowledge);
        return new PageImpl<>(this.knowledgeDao.queryAllByLimit(knowledge, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param knowledge 实例对象
     * @return 实例对象
     */
    @Override
    public Knowledge insert(Knowledge knowledge) {
        this.knowledgeDao.insert(knowledge);
        return knowledge;
    }

    /**
     * 修改数据
     *
     * @param knowledge 实例对象
     * @return 实例对象
     */
    @Override
    public Knowledge update(Knowledge knowledge) {
        this.knowledgeDao.update(knowledge);
        return this.queryById(knowledge.getKnowledgeId());
    }

    /**
     * 通过主键删除数据
     *
     * @param knowledgeId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer knowledgeId) {
        return this.knowledgeDao.deleteById(knowledgeId) > 0;
    }

    // 新增：PageHelper 分页与业务方法
    @Override
    public PageInfo<Knowledge> findPage(Integer pageNum) {
        PageHelper.startPage(pageNum == null || pageNum < 1 ? 1 : pageNum, 10);
        List<Knowledge> list = this.knowledgeDao.selectAll();
        return new PageInfo<>(list);
    }

    @Override
    public void add(Knowledge knowledge) {
        this.knowledgeDao.insert(knowledge);
    }

    @Override
    public void update(Knowledge knowledge, Integer id) {
        knowledge.setKnowledgeId(id);
        this.knowledgeDao.update(knowledge);
    }

    @Override
    public void delete(Integer id) {
        this.knowledgeDao.deleteById(id);
    }

    @Override
    public List<Knowledge> selectByUsername(String username) {
        return this.knowledgeDao.selectByUsername(username);
    }

    @Override
    public Knowledge selectById(Integer id) {
        return this.knowledgeDao.queryById(id);
    }

    @Override
    public PageInfo<Knowledge> findPageByKeys(String keys, Integer pageNum) {
        PageHelper.startPage(pageNum == null || pageNum < 1 ? 1 : pageNum, 10);
        String pattern = keys == null || keys.isEmpty() ? null : "%" + keys + "%";
        List<Knowledge> list = this.knowledgeDao.selectByKeys(pattern);
        return new PageInfo<>(list);
    }
}
